﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Seasar.Framework.Container.Factory;
using Seasar.Framework.Container;
using Shsc.Net.Common.Log;

namespace Shsc.Net.Web.Extension
{
    public class SeasarControllerFactory : DefaultControllerFactory
    {

        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            if (string.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentNullException("controllerName");
            }

            if (controllerName.IndexOf(".") > 0)
            {
                base.CreateController(requestContext, controllerName);
            }
            controllerName = controllerName + "Controller";

            if (SingletonS2ContainerFactory.Container == null)
            {
                throw new ArgumentException("CreateController has been called fedore Configure");
            }

            try
            {
                return (IController)SingletonS2ContainerFactory.Container.GetComponent(controllerName);
            }
            catch (Exception e)
            {
                Log4NetLogger.CreateLogger(this.GetType()).Fatal(e);
                throw new InvalidOperationException("Failed creating instance of: "
                    + controllerName + "using Seasar.Net Object Factory");
            }
        }

    }
}
